def usr_set():
    """
    集合的基本操作
    :return:
    """
    # 定义一个空集合
    set1 = set()
    print(type(set1))  # <class 'set'>

    set2 = {1, 2, 3, 4, 5}  # 不支持随机访问
    print(set2)

    fruits = {"apple", "banana", "cherry"}
    # add:为集合添加新元素
    fruits.add("orange")
    print(fruits)  # {'cherry', 'banana', 'apple', 'orange'}

    fruits = {"apple", "banana", "cherry"}
    # copy：拷贝一个新集合，两集合指向不同的内存单元
    x = fruits.copy()
    print(id(x))  # 1668798735168
    print(id(fruits))  # 1668798731136

    x = {"apple", "banana", "cherry"}
    y = {"google", "microsoft", "apple"}
    # difference:以事件为例：z=x-xy  用一个新的集合z来接收差集
    z = x.difference(y)
    print(id(z))  # 2311458069792
    print(id(x))  # 2311458068672
    print(f'差集{z}')  # 差集{'banana', 'cherry'}

    x = {"apple", "banana", "cherry"}
    y = {"google", "microsoft", "apple"}
    print(id(x))  # 1785528392992
    # difference_update：用旧的集合x来接收集合
    x.difference_update(y)
    print(id(x))  # 1785528392992
    print(x)  # {'banana', 'cherry'}

    fruits = {"apple", "banana", "cherry"}
    fruits.discard("banana")
    # discard:移除集合中指定的元素
    print(fruits)  # {'apple', 'cherry'}

    x = {"a", "b", "c"}
    y = {"c", "d", "e"}
    z = {"f", "g", "c"}
    # intersection：求交集
    result = x.intersection(y, z)
    print(result)

    x = {"apple", "banana", "cherry"}
    y = {"google", "runoob", "apple"}
    # symmetric_difference：用事件来表示：z=x+y-xy
    z = x.symmetric_difference(y)
    print(z)  # {'runoob', 'google', 'banana', 'cherry'}

    x = {"apple", "banana", "cherry"}
    y = {"google", "runoob", "apple"}
    # union:求交集
    z = x.union(y)
    print(z)  # {'runoob', 'google', 'apple', 'banana', 'cherry'}

    print('apple' in z)  # ”apple“是否在集合z中，在则返回True，不在则返回False

    print(x - y)  # x-xy
    print(x | y)  # x+y
    print(x & y)  # xy
    print(x ^ y)  # x+y-xy
    # {'cherry', 'banana'}
    # {'apple', 'cherry', 'runoob', 'google', 'banana'}
    # {'apple'}
    # {'cherry', 'google', 'banana', 'runoob'}


def usr_generator():
    """
    使用生成器生成列表、元组、集合
    :return:
    """
    my_list = [x for x in range(10)]
    print(my_list)
    # 使用生成式生成元组时要强转
    # my_tuple = (x for x in range(10)) #<generator object usr_generator.<locals>.<genexpr> at 0x000001D0B9296680> 报错
    my_tuple = (tuple)(x for x in range(10))
    print(my_tuple)
    # 集合中不重复存储字符串中出现的字符
    my_set = {x for x in 'abracadabra'}
    print(my_set)
    # 集合中不重复存储字符串中出现的字符,且该字符不能出现在”abc“中
    my_set_two = {x for x in 'abracadabra' if x not in "abc"}
    print(my_set_two)


if __name__ == '__main__':
    # usr_set()
    usr_generator()
